package com.zhj.business.controller;

import com.zhj.business.protocol.input.StudentInput;
import com.zhj.business.service.StudentService;
import com.zhj.common.core.result.Result;
import com.zhj.common.core.util.ResultUtils;
import com.zhj.common.web.apiversion.annotation.ApiVersion;
import com.zhj.data.entity.example.Student;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zhj
 */
@Slf4j
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @PostMapping("/add")
    public Result add(@Valid @RequestBody StudentInput studentInput) {
        log.info("接收到的学生信息：" + studentInput);
        Student student = new Student();
        BeanUtils.copyProperties(studentInput, student);
        boolean result = studentService.save(student);
        log.info("保存学生的结果" + result);
        return ResultUtils.createSuccess(student);
    }

    @GetMapping("/list")
    @ApiVersion(0.0)
    public Result<List<Student>> listOld() {
        List<Student> list = new ArrayList<>();
        log.info("这是一个旧的查询接口，无数据返回！！！");
        return ResultUtils.createSuccess(list);
    }

    @GetMapping("/list")
    @ApiVersion(1.0)
    public Result<List<Student>> list() {
        List<Student> list = studentService.list();
        return ResultUtils.createSuccess(list);
    }

    @GetMapping("/list")
    @ApiVersion(2.0)
    public Result<List<Student>> listNew() {
        List<Student> list = new ArrayList<>();
        log.info("这是一个新的查询接口，无数据返回！！！");
        return ResultUtils.createSuccess(list);
    }
}
